<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="margin: 20px 30px 0px"
    >
      <el-form-item>
        <el-input v-model="formInline.loginName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="handleQuery({ page: 1, size: pagination.pageSize })"
          >查询</el-button
        >
        <el-button type="primary" @click="addUser()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="users" style="width: 100%">
      <el-table-column label="登录名" width="200">
        <template #default="scope">
          {{ scope.row.loginName }}
        </template>
      </el-table-column>

      <el-table-column label="名称" width="200">
        <template #default="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="密码" width="400">
        <template #default="scope">
          {{ scope.row.password }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="400">
        <template #default="scope">
         <el-button size="mini" type="primary" @click="handleResetPwdEdit(scope.$index, scope.row)" >重置密码</el-button>&nbsp;&nbsp;
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>&nbsp;&nbsp;
          <el-dialog
            title="用户表单"
            :width="500"
            v-model="dialogFormVisible"
            :modal="false"
          >
            <el-form :model="user" v-loading="loading">
              <el-form-item label="登录名" :label-width="formLabelWidth">
                <el-input
                  v-model="user.loginName" 
                  :disabled="!!user.id"
                ></el-input>
              </el-form-item>
              <el-form-item label="昵称" :label-width="formLabelWidth">
                <el-input v-model="user.name" ></el-input>
              </el-form-item>
              <el-form-item label="密码" :label-width="formLabelWidth"  v-show="!user.id">
                <el-input type="password" v-model="user.password"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave">确 认</el-button>
              </span>
            </template>
          </el-dialog>

           <el-dialog
            title="重置密码"
            :width="500"
            v-model="resetPswFormVisible"
            :modal="false"
          >
            <el-form :model="user" v-loading="resetPwdLoading">
              <el-form-item label="新密码" :label-width="formLabelWidth">
                <el-input type="password" v-model="user.password"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="resetPswFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleResetPswSave">确 认</el-button>
              </span>
            </template>
          </el-dialog>


          <el-popconfirm
            confirmButtonText="确认"
            cancelButtonText="取消"
            icon="el-icon-info"
            iconColor="red"
            title="删除后不可恢复，确认删除?"
            @confirm="handleDelete(scope.$index, scope.row)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="pagination.total"
      :current-page.sync="pagination.current"
      :page-size="pagination.pageSize"
      @current-change="handleTableChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import axios from "axios";
import { ref, reactive } from "vue";
import { defineComponent } from "vue";
import { ElMessage } from "element-plus";
import Tool from "../../util/tool";
export default {
  name: "AdminUser",
  data() {
    return {
      resetPswFormVisible:false,
      resetPwdLoading:false,
      dialogFormVisible: false,
      user: {},
      categoryIds: [],
      formLabelWidth: "120px",
      formComponentSize: "100px",
      loading: false,
    };
  },
  setup() {
    console.log("组件初始化成功");
    const users = ref();
    const pagination = ref({
      current: 1,
      pageSize: 4,
      total: 0,
    });
    const formInline = ref();
    formInline.value = {};

    /**
     * 数据查询
     **/
    const handleQuery = (params) => {
      console.log("loginName=>",formInline.value.loginName)
      let _this = this;
      axios
        .get("/user/list", {
          params: {
            page: params.page,
            size: params.size,
            loginName: formInline.value.loginName,
          },
        })
        .then(function (response) {
          const data = response.data;
          if (data.success) {
            users.value = data.content.list;
            console.log("resp=>data.total:" + data.content.total);
            // 重置分页按钮
            console.log("params=>page=>" + params.page);
            pagination.value.total = data.content.total;
          } else {
            ElMessage.error(data.message);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    };

    /**
     * 表格点击页码时触发
     */
    const handleTableChange = (page) => {
      console.log("看看自带的分页参数都有啥：" + pagination);
      console.log("当前页码：" + pagination.value.current);
      console.log("一页显示的条数：" + pagination.value.pageSize);
      pagination.value.current = page;
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize,
      });
    };

    onMounted: {
      console.log("onMounted=>2222");
      console.log("pagination.current=>" + pagination.value.current);
      console.log("pagination.pageSize=>" + pagination.value.pageSize);
      handleQuery({
        page: 1,
        size: pagination.value.pageSize,
      });
    }

    return {
      users,
      pagination,
      handleTableChange,
      handleQuery,
      formInline,
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log("index:" + index);
      console.log("row", row);
      this.dialogFormVisible = true;
      this.user = Tool.copy(row);
    }, 
    handleResetPwdEdit(index, row) {
      console.log("index:" + index);
      console.log("row", row);
      this.resetPswFormVisible = true;
      this.user = Tool.copy(row);
      this.user.password=null;
    },
    handleDelete(index, row) {
      console.log("==========删除==========");
      console.log("index:" + index);
      console.log("row", row);
      const _this = this;
      _this.axios
        .delete("/user/delete/" + row.id)
        .then(function (response) {
          const data = response.data;
          if (data.success) {
            ElMessage.success({
              message: "操作成功！",
              type: "success",
            });

            //重新加载列表
            _this.handleQuery({
              page: _this.pagination.current,
              size: _this.pagination.pageSize,
            });
          }
        })
        .catch(function (error) {
          ElMessage.error("操作失败！");
          console.log(error);
        });
    },
    handleSave() {
      this.loading = true;
      this.user.password=hexMd5(this.user.password+KEY)
      console.log("pagination.current=>", this.pagination.current);
      const _this = this;
      _this.axios
        .post("/user/save", _this.user)
        .then(function (response) {
          _this.loading = false;
          const data = response.data;
          if (data.success) {
            ElMessage.success({
              message: "操作成功！",
              type: "success",
            });
            _this.dialogFormVisible = false;

            //重新加载列表
            _this.handleQuery({
              page: _this.pagination.current,
              size: _this.pagination.pageSize,
            });
          } else {
            ElMessage.error(data.message);
          }
        })
        .catch(function (error) {
          _this.loading = false;
          _this.dialogFormVisible = false;
          ElMessage.error("操作失败！");
          console.log(error);
        });
    }, 
    handleResetPswSave() {
      this.resetPwdLoading = true;
      this.user.password=hexMd5(this.user.password+KEY)
      console.log("pagination.current=>", this.pagination.current);
      const _this = this;
      _this.axios
        .post("/user/reset-password", _this.user)
        .then(function (response) {
          _this.resetPwdLoading = false;
          const data = response.data;
          if (data.success) {
            ElMessage.success({
              message: "操作成功！",
              type: "success",
            });
            _this.resetPswFormVisible = false;

            //重新加载列表
            _this.handleQuery({
              page: _this.pagination.current,
              size: _this.pagination.pageSize,
            });
          } else {
            ElMessage.error(data.message);
          }
        })
        .catch(function (error) {
          _this.resetPwdLoading = false;
          _this.resetPswFormVisible = false;
          ElMessage.error("操作失败！");
          console.log(error);
        });
    },
    onSubmit() {
      console.log("submit!");
    },
    addUser() {
      this.dialogFormVisible = true;
      this.user = {};
    },
  },
};
</script>

<style>
.el-pagination {
  margin-top: 15px;
  text-align: right;
  margin-bottom: 30px;
}
</style>
